package com.gaotong.science.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaotong.science.entity.HightTechzone;
import com.gaotong.science.entity.HightTechzoneQuery;
import com.gaotong.science.service.HightTechzoneService;
import com.gaotong.science.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suouyang
 * @since 2021-11-03
 */
@RestController
@CrossOrigin
@RequestMapping("/science/hight-techzone")
public class HightTechzoneController {
    @Autowired
    private HightTechzoneService hightTechzoneService;

    @PostMapping("/importFile")
    public R addFile(MultipartFile file) {
        //1 获取上传的excel文件 MultipartFile
        //返回错误提示信息
        hightTechzoneService.importFile(file,hightTechzoneService);
        //判断返回集合是否为空
        return R.ok();
    }
    @GetMapping("selectAll")
    public R selectAll(){
        QueryWrapper<HightTechzone> hightTechzoneQueryWrapper = new QueryWrapper<>();
        hightTechzoneQueryWrapper.orderByDesc("create_time");
        List<HightTechzone> list = hightTechzoneService.list(hightTechzoneQueryWrapper);
        return R.ok().data("data",list);

    }



    @PostMapping("selectCondition")

    public R selectCondition(@RequestBody HightTechzoneQuery hightTechzoneQuery){

        String structure = hightTechzoneQuery.getStructure();
//        String companyName = hightTechzoneQuery.getCompanyName();
        QueryWrapper<HightTechzone>  queryWrapper1 = new QueryWrapper<>();
//        QueryWrapper<HightTechzone>  queryWrapper2 = new QueryWrapper<>();
        if(!StringUtils.isEmpty(structure)){
            queryWrapper1.like("structure", structure).or().like("company_name",structure);


            List<HightTechzone> hightList = hightTechzoneService.list(queryWrapper1);
            return R.ok().data("data",hightList);
            }
        else{
            return R.ok().message("");
        }

    }
    @PostMapping("addHightTechZone")
    public R addHightTechZone(@RequestBody HightTechzone hightTechzone){
        String uuid = UUID.randomUUID().toString().replace("-", "");
//        System.out.println(uuid);
        uuid = uuid.substring(0, 6);
        hightTechzone.setId(uuid);
        boolean flag = hightTechzoneService.save(hightTechzone);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }

    }
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id){
        boolean flag = hightTechzoneService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("updateHightTechZone")
    public R updateHightTechZone(@RequestBody HightTechzone hightTechzone){
        boolean flag = hightTechzoneService.updateById(hightTechzone);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }



}

